﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class DownloadDAO
    {
        public DownloadDAO() { }
        public bool Add(Download down)
        {
            try
            {
                DBHelper.TransSaveOrUpdate(down);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public Download GetDownloadById(int id)
        {
            try
            {
                return DBHelper.ExecuteHQL("from Download where id=" + id).List<Download>().First();
            }
            catch
            {
                return null;
            }
        }
        public IList<Download> GetAllDownload()
        {
            return null;
        }
        public bool ModifyDownload(Download down)
        {
            try
            {
                DBHelper.TransUpdate(down);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteDownloadById(int id)
        {
            try
            {
                DBHelper.ExecuteHQL("delete from Download as di where di.id=" + id).ExecuteUpdate();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public IList<Download> GetDownloadByUserId(int typeId,int pageSize,int currentIndex)
        {
            try
            {
                return  DBHelper.ExecuteHQL(" from Download as di where di.type=" + typeId+" order by id desc").SetFirstResult((currentIndex-1)*pageSize).SetMaxResults(pageSize).List<Download>();
                
            }
            catch
            {
                return null;
            }
        }
        
        public int GetTotal(int type)
        {
            try
            {
                return Convert.ToInt32(DBHelper.ExecuteHQL("select count(*) from Download as di where di.type= " + type).UniqueResult());
            }
            catch
            {
                return 0;
            }
        }

        public IList<Download> GetDownloadByType(int typeId)
        {
            try
            {
                return DBHelper.ExecuteHQL(" from Download as di where di.type=" + typeId + " order by id desc").List<Download>();

            }
            catch
            {
                return null;
            }
        }
    }
}
